﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CLEX.Client.Metro
{
    public static class Helper
    {
        public static string GetServerNameFromConnectionString(string connectionString)
        {
            Match match = Regex.Match(connectionString, "Data Source=.*?;");
            string server = match.Value.Substring("Data Source=".Length, match.Value.Length - "Data Source=;".Length);
            return server;
        }

        public static string GetDatabaseNameFromConnectionString(string connectionString)
        {
            Match match = Regex.Match(connectionString, "Initial Catalog=.*?;");
            string server = match.Value.Substring("Initial Catalog=".Length, match.Value.Length - "Initial Catalog=;".Length);
            return server;
        }

        public static IEnumerable<List<T>> GetPowerSet<T>(this IEnumerable<T> collection)
        {
            //https://gist.github.com/gabehesse/952517

            var list = collection.ToList();
            var result = new List<List<T>>();
            for (int i = 0; i < (1 << list.Count); i++)
            { // 1 << n == the n-th power of 2
                var sublist = new List<T>();
                for (int j = 0; j < list.Count; j++)
                { // analyze each bit in "i"
                    if ((i & (1 << j)) != 0)
                    { // if the j-th bit of i is set...
                        sublist.Add(list[j]); // add the item to the current sublist
                    }
                }
                yield return sublist;
            }
        }

        public static void AddRange<T>(this IList<T> list, IEnumerable<T> range)
        {
            foreach (T item in range)
            {
                list.Add(item);
            }
        }
    }
}
